import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int size;

    public MyStack(){
        elem = new int[5];
    }

    public int push(int val){
        checkCapacity();
        elem[size] = val;
        size++;
        return val;
    }

    public int pop(){
        try{
            if(isEmpty())
                throw new EmptyStackException("栈为空！");
        } catch (EmptyStackException e) {
            e.printStackTrace();
        }
        int ret = elem[size-1];
        size--;
        return ret;
    }


    public void checkCapacity(){
        if(size==elem.length){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
    }

    //获取栈底元素但不删除
    public int peek(){
        try{
            if(isEmpty())
                throw new EmptyStackException("栈为空！");
        } catch (EmptyStackException e) {
            e.printStackTrace();
        }
        return elem[size-1];
    }

    public boolean isEmpty(){
        return size==0;
    }


}
